#!/bin/sh
set -eu

if [ $# -lt 1 ]; then
  echo 'No task is specified.' >&2
  exit 1
fi

get_external_args() {
  while [ $# -gt 0 ]; do
    if [ "$1" = '--' ]; then
      shift
      break
    fi
    shift
  done
  echo "$@"
}

cd "$(dirname "$0")"

case $1 in
  'c'|'configure' )
    cmake -Bbuild -GNinja -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=vcpkg/scripts/buildsystems/vcpkg.cmake "$(tools/cmake-args)" $(get_external_args "$@")
    ;;
  'b'|'build' )
    cmake --build build
    ;;
  'r'|'run' )
    ./build/"$2" $(get_external_args "$@")
    ;;
  't'|'test' )
    ctest -V --test-dir build
    ;;
  'lint' )
    clang-tidy -p build examples/mix/main.cc examples/extract/main.cc
    ;;
  * )
    echo "Unrecognized task: $1" >&2
    exit 1
    ;;
esac
